package.cpath = "./luaclib/?.so"
package.path = package.path .. ';/home/ubuntu/IMDT_GameDev/?.lua';

if _VERSION ~= "Lua 5.4" then error "Use lua 5.4" end

local socket = require "client.socket"
require "src.util"

local port = 8960
local ip = "43.136.68.19"

local fd = assert(socket.connect(ip, port))

local function send_str(strsent) socket.send(fd, strsent) end

local function message_type() end

-- socket.send(fd, "Hello world") -- 向服务器发送消息

print("请输入角色ID")

local hasLoggedIn = false
local input_id = nil
local name = nil

local receiveQueue = Queue:new()

-- 协议工具函数
-- 服务器返回字符串，以|为分割，第一段为状态码，1成功0失败，后面为返回内容
-- 阻塞地缓存服务器返回的内容
local function receive()
    local res = readServer(1, fd)
    if res == nil then return end
    res = string_split(res, "\n")
    for i, v in ipairs(res) do if v ~= nil then receiveQueue:Enqueue(v) end end
end

local function executeRes()
    -- 从队列中取出指令处理
    local res = receiveQueue:Dequeue()
    if res == nil then
        receive()
        -- print("哪里不对>.<  " .. "服务器暂无响应")
        return nil
    end
    res = string_split(res, "|")
    local code = tonumber(res[1])
    if code == 1 then
        -- 执行成功，返回内容
        return res[2]
    elseif code == 0 then
        -- 执行失败，显示原因
        print("哪里不对>.<  " .. res[2])
        return nil
    end
end

-- 要求用户输入ID登录
while not name do
    local input = readStdinNumber()
    socket.send(fd, "system-login|" .. input)

    receive()
    local res = executeRes()
    -- 此处接受服务器发回的名字
    if res ~= nil then name = res end
end

function printInfo()
    print("-----------------------------------------------")
    print(
        "输入指令进行操作，1:根据ID删除角色，2:输入数字n查询攻击力前n的角色，quit:退出")
end

print("欢迎回来，尊敬的【" .. name .. "】大人^.^")
printInfo()

-- 删除角色
function deleteRole()
    print("请输入要删除的角色id")
    local roleId = readStdinNumber()

    socket.send(fd, "player-delete-role|" .. roleId)

    receive()
    local res = executeRes()
    if res ~= nil then
        -- 接受删除的角色信息
        print("成功删除角色:" .. res)
    end
    printInfo()
end

-- 请求攻击力前n的角色
function requestRolesAtk(n)
    print("按攻击力排名查询角色，请输入要查询的数量")
    local num = readStdinNumber()

    socket.send(fd, "player-request-roles-atk|" .. num)

    -- 接收了一堆
    receive()
    local num = tonumber(executeRes())
    if type(num) == 'number' then
        for i = 1, num, 1 do
            if i % 50 == 0 then receive() end
            local info
            while info == nil do info = executeRes() end
            print(i, info)
            socket.usleep(10000)
        end
    end
    printInfo()
end

while true do
    -- 以不阻塞的方式读取用户输入的消息，以便同时监听服务器
    -- client.socket导致io.read()行为被改变的原理 https://github.com/cloudwu/skynet/issues/539
    local readstr = socket.readstdin()
    if readstr then
        if readstr == "quit" then
            print("跟你玩很开心，【" .. name .. "】，下次见啦:D")
            socket.close(fd)
            break
        elseif readstr == "1" then
            deleteRole()
        elseif readstr == "2" then
            requestRolesAtk()
        else
            print("不是有效指令")
            printInfo()
        end
    else
        socket.usleep(10000) -- 单位是微秒
    end
end
